/**
 * 
 */
package cn.com.yinhex.util;

import javax.swing.ImageIcon;

public class Imagick extends CommandUtil {

    public Imagick() {
      super();
    }

    public String command = "/usr/bin/convert";

    /**
     * 按照高宽比例缩小图片
     * @param filename 源图片
     * @param newname 目标图片
     * @param width 目标图片宽度
     * @param height 目标图片高度
     * @return
     */
    public boolean scale(String filename, String newname, int width, int height) {
      return process(command + " " + filename + " -scale " + width + "x" + height
          + " " + newname);
    }

    /**
     * 按照高宽比例缩小图片
     * @param filename 源图片
     * @param newname 目标图片
     * @param width 目标宽度
     * @param height 目标高度
     * @return
     */
    public boolean thumbnail(String filename, String newname, int width,
        int height) {
      return process(command + " " + filename + " -thumbnail " + width + "x"
          + height + " " + newname);
    }

    /**
     * 文字水印(经多次调试在java调用失败,建议把命令行写到shell脚本,再调用脚本执行)
     * @param filename
     * @param newname
     * @param text
     * @param font
     * @param size
     * @param top
     * @param left
     * @return
     */
    public boolean drawText(String filename, String newname, String text,
        String font, int size, int top, int left) {
      return process(command + " " + filename + " -font " + font + " -pointsize "
          + size + " -draw \"text " + left + "," + top + " '`echo \"" + text
          + "\"|iconv -f \"GB2312\" -t \"UTF-8\"`'\" " + newname);
    }

    /**
     * 加图片水印
     * @param filename 源图片
     * @param newname 目标图片
     * @param logofile 水印图片
     * @param dissolve 和水印的融合度,0-100的数字
     * @param gravity 叠放方向,East,West,North,South,NorthEast,NorthWest,SouthEast,SouthWest
     * @return
     */
    public boolean drawLogo(String filename, String newname, String logofile,
        int dissolve, String gravity) {
      return process("composite -dissolve " + dissolve + "% -gravity " + gravity
          + " " + logofile + " " + filename + " " + newname);
    }
    
    
    /**
     * 加图片水印
     * @param filename 源图片
     * @param newname 目标图片
     * @param logofile 谁赢图片
     * @param dissolve 和水印的融合度,0-100的数字
     * @param x 希望距离右下角的距离
     * @param y 希望距离右下角的距离
     * @return
     */
    public boolean drawLogo(String filename, String newname, String logofile,
        int dissolve, int x,int y) {
      ImageIcon icon = new ImageIcon(filename);   
      int width=icon.getIconWidth(); //得到源图宽 
      int height=icon.getIconHeight(); //得到源图长

      String _x = String.valueOf(width-x);//在x轴上水印图片的左上顶点距离图片左上角的距离
      String _y = String.valueOf(height-y);//在y轴上水印图片的左上顶点距离图片左上角的距离
      
      return process("composite -dissolve " + dissolve + "% -geometry +" + _x + "+" + _y 
          + " " + logofile + " " + filename + " " + newname);
    }
    

    /**
     * 裁剪图片
     * @param filename 源图片
     * @param newname 目标图片
     * @param width 目标宽度
     * @param height 目标高度
     * @param left 裁剪位置：距离左边的像素
     * @param top 裁剪位置：距离上边的像素
     * @return
     */
    public boolean crop(String filename, String newname, int width, int height,
        int left, int top) {
      return process(command + " " + filename + " -crop " + width + "x" + height
          + "+" + left + "+" + top + " " + newname);
    }

    /**
     * 加边框
     * @param filename
     * @param newname
     * @param borderwidth
     * @param borderheight
     * @param bordercolor
     * @return
     */
    public boolean border(String filename, String newname, int borderwidth,
        int borderheight, String bordercolor) {
      return process(command + " -bordercolor " + bordercolor + " -border "
          + borderwidth + "x" + borderheight + " " + filename + " " + newname);
    }

    /**
     * 转换图片格式
     * @param filename
     * @param newname
     * @param format
     * @return
     */
    public boolean format(String filename, String newname, String format) {
      return process(command + " " + filename + " -format \"" + format + "\" "
          + newname);
    }
    
    /**
     * 获取矩形的小图
     * @param filename
     * @param newname
     * @param w 小图宽度
     * @param h 小图高度
     * @param left 裁剪位置：距离左边的像素
     * @param top 裁剪位置：距离上边的像素
     * @return
     */
    public boolean cropRect(String filename,String newname,int w,int h,int left,int top){
      ImageIcon icon = new ImageIcon(filename);   
      int width=icon.getIconWidth(); //得到源图宽 
      int height=icon.getIconHeight(); //得到源图高
      int[] s = new int[2];
      if(width < height){//以宽为标准
        s = getSize(width, height, w, h, 1);
      }
      else{//以高为标准
        s = getSize(width, height, w, h, 2);
      }
      
      if(thumbnail(filename,newname,s[0],s[1])){
        return crop(newname,newname,w,h,left,top);
      }
      return false;
    }
    /**
     * 获得要压缩的图片的大小
     * 
     * @param w
     *            图片原宽度
     * @param h
     *            图片原高度
     * @param width
     *            标准宽
     * @param height
     *            标准高
     * @param type
     *            类型 1-以宽为标准压缩 2-以高为标准压缩 3-以比例大小压缩
     * @return size[0]-要压缩的宽度 size[1]-要压缩的高度
     */
    public static int[] getSize(double w, double h, double width, double height,
        int type) {
      if (w < width) {// 如果原宽度比标准宽度小
        width = w;
      }
      if (h < height) {// 如果原高度比标准高度小
        height = h;
      }
      double scale = w / (double) h;
      switch (type) {
      case 1:
        height = width / scale;
        break;
      case 2:
        width = height * scale;
        break;
      case 3:
        if (width / height > scale) {
          width = height * scale;
        } else if ((width / height) < scale) {
          height = width / scale;
        }
        break;
      }
      int[] size = new int[2];
      size[0] = (int) width;
      size[1] = (int) height;
      return size;

    }
    public static void main(String[] args) {
    }
}